package clientApp.utils;

import game.map.MapInteraction;
import game.map.MapItem;
import game.map.MapUnit;
import game.map.Static;
import game.map.WsApplet;
import game.map.mapEvents.MapEvent;
import game.units.Unit;

import java.awt.CardLayout;
import java.util.ArrayList;

import communication.client.PlayerInteractionManager;
import communication.common.LoggedPlayers;
import communication.server.MasterInteractionManager;

import clientApp.InventoryMenu;

/**
 * 
 * @author Gabriel
 */
@SuppressWarnings("serial")
public class Game_Container extends javax.swing.JPanel {

	public CardLayout panels;
	private WsApplet game;
	public InventoryMenu invMenu;
	public PlayerInteractionManager player;
	public MasterInteractionManager master;
	public boolean isPlayer;

	/** Creates new form RPG_Workstation */
	public Game_Container(RPG_Workstation_Container container, boolean isPlayer) {
		this.isPlayer = isPlayer;
		LoggedPlayers loggedPlayers = null;

		if (isPlayer) {
			player = container.player;
			panels = new CardLayout();
			this.setLayout(panels);
			game = player.getApplet();
			loggedPlayers = player.getLoggedPlayers();

			ArrayList<MapUnit> allMapUnits = new ArrayList<MapUnit>();

			for (int i = 0; i < loggedPlayers.getNumPlayers(); i++) {
				if (!loggedPlayers.get(i).getName().equals(player.getCharacter().getName())) {
					MapUnit mapUnit = new MapUnit(0, 0, loggedPlayers.get(i).getColor(), 1, new Unit());
					mapUnit.setId(loggedPlayers.get(i).getId());
					allMapUnits.add(mapUnit);
				}
			}

			allMapUnits.add(new MapUnit(0, 0, Static.CYAN, 1, player.getCharacter()));

			game.loadPlayersMap(player.getCurrentMapName(), allMapUnits, new ArrayList<MapItem>(), new ArrayList<MapEvent>(), player.getCharacter().getId());
			game.init();
			add(game, "Game");
		} else {
			master = container.master;
			panels = new CardLayout();
			this.setLayout(panels);
			game = master.getApplet();
			loggedPlayers = master.getLoggedPlayers();

			ArrayList<MapUnit> allMapUnits = new ArrayList<MapUnit>();

			// TODO: passar unit correta
			for (int i = 0; i < loggedPlayers.getNumPlayers(); i++) {
				MapUnit mapUnit = new MapUnit(0, 0, loggedPlayers.get(i).getColor(), 1, new Unit());
				mapUnit.setId(loggedPlayers.get(i).getId());
				allMapUnits.add(mapUnit);
			}
			// TODO: passar unit correta
			game.loadPlayersMap(master.getCurrentMapName(), allMapUnits, new ArrayList<MapItem>(), new ArrayList<MapEvent>(), 0);
			game.init();
			add(game, "Game");
		}

	}

	public MapInteraction getMapInteraction() {
		return game.getMapInteraction();
	}

	public InventoryMenu getInvMenu() {
		return invMenu;
	}

	public void setInvMenu(InventoryMenu invMenu) {
		this.invMenu = invMenu;
		this.add(invMenu, "Inventory");
	}

}
